package com.cardbaobao.cardbabyclient.ui.fragment.applyrecord;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.api.Api;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.buried.SelfUtils;
import com.cardbaobao.cardbabyclient.ui.activity.SpeedLoanActivity;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.ui.adapter.LoansApplyRecordRcAdapter;
import com.cardbaobao.cardbabyclient.ui.fragment.base.BaseFragment;
import com.cardbaobao.cardbabyclient.model.LoansApplyRecord;
import com.cardbaobao.cardbabyclient.model.result.ApplyRecordResult;
import com.cardbaobao.cardbabyclient.utils.ApplicationHelper;
import com.cardbaobao.cardbabyclient.utils.JsonUtil;
import com.cardbaobao.cardbabyclient.utils.ToastUtil;
import com.cardbaobao.cardbabyclient.utils.UmengEventUtils;
import com.cardbaobao.cardbabyclient.widget.RecyclerViewWithFooter.OnLoadMoreListener;
import com.cardbaobao.cardbabyclient.widget.RecyclerViewWithFooter.RecyclerViewWithFooter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import rx.Observable;
import rx.Subscriber;

/**
 * Description: 贷款申请记录
 * Created by FHW
 * on 2017/7/4
 */

public class LoansApplyRecordFragment extends BaseFragment {
    private String prePageId;
    public void setBuriedInfo(String prePageId){
        this.prePageId=prePageId;
    }

    @BindView(R.id.srLayout)
    SmartRefreshLayout srLayout;

    @BindView(R.id.rv_credit_record)
    RecyclerViewWithFooter rv_credit_record;
    @BindView(R.id.date_no_layouts)
    LinearLayout date_no_layouts;
    @BindView(R.id.commit_data)
    TextView commit_data;//申请信用卡


    private List<LoansApplyRecord> loansApplyRecordList = new ArrayList<>();//贷款申请记录
    private LoansApplyRecordRcAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_loans_apply_record;
    }

    public void onResume() {
        super.onResume();
        UmengEventUtils.pageEvent(mContext,
                ApplicationHelper.getApplicationContext().getString(R.string.event_0x019));
    }

    @Override
    protected void initViewFinish(View view, Bundle savedInstanceState) {

        mCurrentPageId= Self.Page.app_loan_apply_record_page;

        srLayout.setEnableLoadmore(false);

        mAdapter = new LoansApplyRecordRcAdapter(getActivity());
        rv_credit_record.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv_credit_record.setAdapter(mAdapter);

        getLoansApplyRecordData(true);

        srLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                onRefreshRv();
            }
        });

        rv_credit_record.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                rv_credit_record.setEnd("没有更多了");
            }
        });

        commit_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //去逛逛
                SelfUtils.buriedClickData(Self.Action.app_click_loan_apply_record_view_other,
                        Self.Action.app_click_loan_apply_record_view_other_des,
                        mEnterPageTime,
                        mCurrentPageId,
                        prePageId);
                Intent intent = new Intent(mContext, SpeedLoanActivity.class);
                intent.putExtra(BaseActivity.PRE_PAGE_ID_S,mCurrentPageId);
                startActivity(intent);
                getActivity().finish();
            }
        });

    }

    private void onRefreshRv() {
        rv_credit_record.setEmpty();
        getLoansApplyRecordData(false);
    }

    /**
     * 获取信用卡申请记录
     */
    private void getLoansApplyRecordData(boolean isShowDialog) {
        if (isShowDialog) {
            showProgressDialog(false);
        }
        Observable<String> observable=
                Api.loanApplyRecord(String.valueOf(mApplication.getUser().getID()));
        observable.subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.showToast(ApplicationHelper.
                        getApplicationContext().
                        getString(R.string.string_request_server_fail));
                dismissProgressDialog();
                srLayout.finishRefresh();
            }

            @Override
            public void onNext(String s) {
                ApplyRecordResult applyRecordResult = JsonUtil.json2Object(s, ApplyRecordResult.class);
                if (applyRecordResult.getData().getDataList() != null) {
                    loansApplyRecordList.clear();
                    loansApplyRecordList.addAll(applyRecordResult.getData().getDataList());
                }
                rv_credit_record.setEnd("没有更多了");
                if (loansApplyRecordList.size() > 0) {
                    mAdapter.setData(loansApplyRecordList);
                    hideNoDataLayout();
                } else {
                    showNoDataLayout();
                }
                dismissProgressDialog();
                srLayout.finishRefresh();
            }
        });
    }

    /**
     * //展示没有数据Ui
     */
    private void showNoDataLayout() {
        if (srLayout != null && date_no_layouts != null) {
            date_no_layouts.setVisibility(View.VISIBLE);
            srLayout.setVisibility(View.GONE);
        }
    }

    /**
     * 隐藏没有数据Ui
     */
    private void hideNoDataLayout() {
        if (srLayout != null && date_no_layouts != null) {
            date_no_layouts.setVisibility(View.GONE);
            srLayout.setVisibility(View.VISIBLE);
        }
    }
}
